"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】GRANT/REVOKE命令触发事件触发器
Create At   : 2024/10/15
Owner       : sungang14
Description :
    预置条件：
        1.创建事件触发器：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0004()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
        CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0004();
        CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0004();
        2.创建表和用户
        CREATE TABLE test1(id int, col int);
        CREATE USER testuser  PASSWORD 'Bigdata@123';
    测试步骤：
        1、GARNT授予权限命令执行：GRANT ALL ON TABLE test1 TO testuser;
        2、REVOKE撤销权限命令执行：REVOKE ALL ON TABLE test1 FROM testuser;
Expect      : 
    1、触发执行函数f_eventtrigger_0004()
     test_event_trigger: ddl_command_start,GRANT
     test_event_trigger: ddl_command_end,GRANT
    2、分别触发执行函数f_eventtrigger_0004()
     test_event_trigger: ddl_command_start,REVOKE
     test_event_trigger: ddl_command_end,REVOKE
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.fun_name = "f_eventtrigger_0004()"
        self.user_name = "u_eventtrigger_0004"
        self.t_name = "t_eventtrigger_0004"
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.comsh.execut_db_sql(f'drop function if exists {self.fun_name};')
        self.comsh.execut_db_sql(f'drop role if exists {self.user_name};')
        self.comsh.execut_db_sql(f'drop table if exists {self.t_name};')
        msg = self.comsh.execut_db_sql(f"CREATE TABLE  {self.t_name} (manager text, company text, contact_email text);")
        self.logger.info(msg)
        self.assertEqual("CREATE TABLE", msg)
        msg = self.comsh.execut_db_sql(f"create ROLE {self.user_name} password 'Huawei@123';")
        self.logger.info(msg)
        self.assertEqual(f'CREATE ROLE', msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION {self.fun_name} RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertEqual("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)

    def test_Index_file_damaged(self):
        self.logger.info("GARNT授予权限命令执行：GRANT ALL ON TABLE test1 TO testuser;")
        msg = self.comsh.execut_db_sql(f"GRANT ALL ON TABLE {self.t_name} TO {self.user_name};")
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start GRANT', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end GRANT', msg)

        self.logger.info('REVOKE撤销权限命令执行：REVOKE ALL ON TABLE test1 FROM testuser;')
        msg = self.comsh.execut_db_sql(f'REVOKE ALL ON TABLE {self.t_name} FROM {self.user_name};')
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start REVOKE', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end REVOKE', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql(f"drop function if exists {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        # 删除表
        msg = self.comsh.execut_db_sql(f"drop TABLE if exists {self.t_name};")
        self.logger.info(msg)
        self.assertIn('DROP TABLE', msg)
        # 删除用户
        msg = self.comsh.execut_db_sql(f"drop USER if exists {self.user_name};")
        self.logger.info(msg)
        self.assertIn('DROP ROLE', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
